home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 180 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  83 lines

  1. Path: ix.netcom.com!netnews
  2. From: tjensen@ix.netcom.com (Ted Jensen)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers to structures
  5. Date: Wed, 03 Jan 1996 02:27:02 GMT
  6. Organization: Netcom
  7. Message-ID: <4ccpgv$qpl@ixnews8.ix.netcom.com>
  8. References: <4cc9r4$m26@armitage.cyberspace.com>
  9. Reply-To: tjensen@ix.netcom.com
  10. NNTP-Posting-Host: pax-ca7-05.ix.netcom.com
  11. X-NETCOM-Date: Tue Jan 02  6:26:39 PM PST 1996
  12. X-Newsreader: Forte Free Agent 1.0.82
  13.  
  14. icarus@loomis (Tel Janin Aellinsar) wrote:
  15.  
  16. >Berserker Dragon, Knights of the Cosmos             icarus@BERKSHIRE.NET
  17.  
  18. > I'm having trouble using a pointer to a struct.  The idea is to
  19. > have a struct get filled by a function, which gets the address
  20. > and everything via function(struct type*).  Very straightforward.
  21. > However, if I try to CHANGE anything in the struct, it just goes
  22. > back when the function is over.  So, let's pretend this imaginary
  23. > structure is what I'm using:
  24. >
  25. > struct st1 {
  26. >   char *name;
  27. >   int yadda;
  28. > };
  29. >
  30. > And the function is:
  31. >
  32. > fn1(struct st1 *st)
  33. > {
  34. >   st = (struct st1*)malloc(sizeof(struct st1*));
  35. >   st->name = (char*)malloc(16);
  36. >   strcpy(st->name,"Test");
  37. >   st->yadda = 100;
  38. > }
  39.  
  40. Well you have several things wrong here.  Since you haven't
  41. posted a main() I don't know exactly how you go about calling
  42. your function or what you expect it to return.  As things stand,
  43. you pass the function the value of a pointer.  Recall that this
  44. becomes local to the function and disappears on exiting the
  45. function.  Thus, while you are allocating the space, you have not
  46. provided a way for the pointer to that space to get returned to
  47. the calling function.
  48.  
  49. One way to get the pointer to the allocated memory back to the
  50. calling program is to write the function as:
  51.  
  52. struct st1 *fn1(struct st1 *st)
  53. {
  54.   st = (struct st1 *)malloc(sizeof(struct st1*));
  55.   st->name = (char*)malloc(16);
  56.   strcpy(st->name,"Test");
  57.   st->yadda = 100;
  58.   return st;
  59. }
  60.  
  61. Here I have changed the function prototype so that the function
  62. is expected to return the pointer to the allocated space, and
  63. added the return statement at the end of the function.
  64.  
  65. Remember that you need to allocate space for this pointer in the
  66. calling function before you assign the value returned by this
  67. function.
  68.  
  69. For more details on the use of pointers, see my tutorial on same
  70. which is named PTRTUT01.ZIP and can be found at one of the
  71. following locations:
  72.  
  73. http://oak.oakland.edu:8080/SimTel/msdos/c/ptrtut01.zip
  74.  
  75. ftp.coast.net/SimTel/msdos/c/ptrtut01.zip
  76.  
  77. Hope this helps!
  78.  
  79. Ted Jensen                 Author of PTRTUT01.ZIP
  80. Redwood City, CA      A tutorial on C pointers and Arrays
  81. tjensen@ix.netcom.com    Available via Simtel/msdos/c
  82.  
  83.